﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using homework2_bank.classes;

namespace homework2_bank
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window , INotifyPropertyChanged

    {
        /**
         * 逻辑参数
         */
        public static MainWindow win;
        ATM thisATM = new ATM();

        /**
         * 绑定参数
         */
        public event PropertyChangedEventHandler PropertyChanged;
        //显示屏
        private string _screenLabel;
        public string ScreenLabel
        {
            set
            {
                _screenLabel = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ScreenLabel"));
                }
            }
            get
            {
                return _screenLabel;
            }
        }
        // 账户余额
        private int _leftBalance;
        public int LeftBalance
        {
            set
            {
                _leftBalance = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("LeftBalance"));
                }
            }
            get
            {
                return _leftBalance;
            }
        }
        // 信用额度
        private int _creditBalance;
        public int CreditBalance
        {
            set
            {
                _creditBalance = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("CreditBalance"));
                }
            }
            get
            {
                return _creditBalance;
            }
        }
        // 可用额度
        private int _availableBalance;
        public int AvailableBalance
        {
            set
            {
                _availableBalance = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AvailableBalance"));
                }
            }
            get
            {
                return _availableBalance;
            }
        }
        // 已存现金
        private int _insideCash;
        public int InsideCash
        {
            set
            {
                _insideCash = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("InsideCash"));
                }
            }
            get
            {
                return _insideCash;
            }
        }
        // 吐出现金
        private int _outCash;
        public int OutCash
        {
            set
            {
                _outCash = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("OutCash"));
                }
            }
            get
            {
                return _outCash;
            }
        }
        //银行名称
        public string BankName
        {
            set
            {
                thisATM.CurrentBank = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("BankName"));
                }
            }
            get
            {
                return thisATM.CurrentBank;
            }
        }
        //AccountInput,PasswordInput,WithdrawalInput

        /**
         * 交互函数
         */
        //程序初始化
        public MainWindow()
        {
            InitializeComponent();
            win = this;
            ScreenLabel = "欢迎使用!";
            DataContext = this;
        }

        //退出程序
        private void Image_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.Close();
        }

        //插卡操作
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            thisATM.ReadCard();
        }

        //登陆操作
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string account = AccountInput.Text;
            string password = PasswordInput.Text;
            thisATM.Login(account, password);
        }

        //查询操作
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            thisATM.GetBalance();
        }

        //模拟放钱
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            Cash cash = new Cash(100,0);
            thisATM.MoneyIn(cash);
        }

        //模拟拿钱
        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            OutCash = 0;
        }

        //存款
        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            thisATM.SaveMoney(InsideCash);
        }

        //取款
        private void Button_Click_6(object sender, RoutedEventArgs e)
        {
            int result;
            if (int.TryParse(WithdrawalInput.Text,out result))
            {
                thisATM.WithdrawalMoney(int.Parse(WithdrawalInput.Text));
            }
            else
            {
                ScreenLabel = "请输入合理的取款数值";
            }
        }

        //自定义拖动
        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }
    }
}
